package com.fire.control.system.module.middleware.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.entity.Middleware;
import com.fire.control.system.module.middleware.dao.MiddleWareMapper;
import com.fire.control.system.module.middleware.dao.MiddleWareRepository;
import com.fire.control.system.module.middleware.service.IMiddleWareService;
import com.fire.control.system.statics.Message_Response;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.middleware.MiddleWareRequestProtocol;
import com.fire.control.system.support.util.ResultPageUtil;
import com.github.pagehelper.PageInfo;

@Service
public class MiddleWareServiceImpl implements IMiddleWareService {
	public static final Logger logger = LoggerFactory.getLogger(MiddleWareServiceImpl.class);

	@Autowired
	private MiddleWareMapper middleWareMapper;
	
	@Autowired
	private MiddleWareRepository middleWareRepository;
	
	@Autowired
	private ProtocolSelector protocolSelector;
	
	@Autowired
	private ResultPageUtil resultPageUtil;
	
	/**
	 * <p>Title: getMiddleWareList</p>
	 * <p>Description: 获得中间件列表</p>
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.middleware.service.IMiddleWareService#getMiddleWareList(com.fire.control.system.support.protocol.middleware.MiddleWareRequestProtocol)
	 */
	@Override
	public ResponseProtocol getMiddleWareList(MiddleWareRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol responseProtocol = protocolSelector.choose(requestProtocol);
		
		List<Middleware> middleWareLise = middleWareMapper.getMiddlewareList(requestProtocol);
		
		Map<String,Object> middleWarePage = resultPageUtil.resultPage(new PageInfo<Middleware>(middleWareLise), middleWareLise);
		if(middleWareLise.size()>0 && !middleWareLise.equals(null)){
			ResponseMessage.CODE_200.messageToProtocol(responseProtocol);
			responseProtocol.setData(middleWarePage);
		}else{
			Message_Response.FAIL.messageToProtocol(responseProtocol);
		}
		return responseProtocol;
	}

	/**
	 * <p>Title: addMiddleWare</p>
	 * <p>Description: 添加中间件</p>
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.middleware.service.IMiddleWareService#addMiddleWare(com.fire.control.system.support.protocol.middleware.MiddleWareRequestProtocol)
	 */
	@Override
	public ResponseProtocol addMiddleWare(MiddleWareRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol responseProtocol = protocolSelector.choose(requestProtocol);
		
		Middleware middleware = requestProtocol.getMiddleware();
		
		middleware.setId(UUID.randomUUID().toString());
		
		int pk = middleWareMapper.addMiddleWare(middleware);
		
		if(!Integer.valueOf(pk).equals(null)){
			ResponseMessage.CODE_200.messageToProtocol(responseProtocol);
			responseProtocol.setData(pk);
		}else{
			Message_Response.FAIL.messageToProtocol(responseProtocol);
		}
		return responseProtocol;
	}

	/**
	 * <p>Title: deleteMiddleWare</p>
	 * <p>Description: 根据Id删除中间件</p>
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.middleware.service.IMiddleWareService#deleteMiddleWare(com.fire.control.system.support.protocol.middleware.MiddleWareRequestProtocol)
	 */
	@Override
	public ResponseProtocol deleteMiddleWare(MiddleWareRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol responseProtocol = protocolSelector.choose(requestProtocol);
		
		String[] ids = requestProtocol.getMiddlewareIds();
		
		int pk = middleWareMapper.deleteMiddleWare(ids);
		
		if(!Integer.valueOf(pk).equals(null)){
			ResponseMessage.CODE_200.messageToProtocol(responseProtocol);
			responseProtocol.setData("success");
		}else{
			Message_Response.FAIL.messageToProtocol(responseProtocol);
		}
		return responseProtocol;
	}

	/**
	 * <p>Title: updateMiddleWare</p>
	 * <p>Description: 修改中间件信息</p>
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.middleware.service.IMiddleWareService#updateMiddleWare(com.fire.control.system.support.protocol.middleware.MiddleWareRequestProtocol)
	 */
	@Override
	public ResponseProtocol updateMiddleWare(MiddleWareRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol responseProtocol = protocolSelector.choose(requestProtocol);
		
		String id = requestProtocol.getMiddlewareId();
		Middleware middleware = middleWareRepository.findOne(id);
		
		Middleware paramMiddleWare = requestProtocol.getMiddleware();
		
		middleware.setMiddleware(paramMiddleWare);
		
		Middleware returnParm = middleWareRepository.saveAndFlush(paramMiddleWare);
		
		if(!returnParm.equals(null)){
			ResponseMessage.CODE_200.messageToProtocol(responseProtocol);
			responseProtocol.setData("success");
		}else{
			Message_Response.FAIL.messageToProtocol(responseProtocol);
		}
		return responseProtocol;
	}

}
